package com.memory.monitor.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.memory.monitor.common.DeleteRequest;
import com.memory.monitor.common.ErrorCode;
import com.memory.monitor.constant.ExcelConstant;
import com.memory.monitor.exception.BusinessException;
import com.memory.monitor.exception.ThrowUtils;
import com.memory.monitor.job.AreaExcelListener;
import com.memory.monitor.job.MarketExcelListener;
import com.memory.monitor.job.MenutExcelListener;
import com.memory.monitor.mapper.MenuMapper;
import com.memory.monitor.model.dto.menu.MenuAddRequest;
import com.memory.monitor.model.dto.menu.MenuQueryRequest;
import com.memory.monitor.model.dto.menu.MenuUpdateRequest;
import com.memory.monitor.model.entity.Market;
import com.memory.monitor.model.entity.Menu;
import com.memory.monitor.model.entity.User;
import com.memory.monitor.model.vo.MarketVO;
import com.memory.monitor.model.vo.MenuVO;
import com.memory.monitor.service.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.memory.monitor.constant.ExcelConstant.*;

/**
* @author 21731
* @description 针对表【menu(菜单管理)】的数据库操作Service实现
* @createDate 2024-09-02 09:59:58
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService {

    @Override
    public MenuVO getMenuVO(Menu menu) {
        if (menu == null) {
            return null;
        }
        MenuVO menuVO = new MenuVO();
        BeanUtils.copyProperties(menu, menuVO);
        return menuVO;
    }

    @Override
    public List<MenuVO> getMenuVO(List<Menu> menuList) {
        if (CollectionUtils.isEmpty(menuList)) {
            return new ArrayList<>();
        }
        return menuList.stream().map(this::getMenuVO).collect(Collectors.toList());
    }

    @Override
    public QueryWrapper<Menu> getQueryWrapper(MenuQueryRequest menuQueryRequest) {
        if (menuQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = menuQueryRequest.getId();
        String menuName = menuQueryRequest.getMenuName();
        String menuIcon = menuQueryRequest.getMenuIcon();
        String type = menuQueryRequest.getType();
        Integer orderId = menuQueryRequest.getOrderId();
        Integer status = menuQueryRequest.getStatus();
        String flag = menuQueryRequest.getFlag();
        String route = menuQueryRequest.getRoute();

        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(menuName), "menu_name", menuName);
        queryWrapper.like(StringUtils.isNotBlank(menuIcon), "menu_icon", menuIcon);
        queryWrapper.like(StringUtils.isNotBlank(type), "type", type);
        queryWrapper.eq(orderId != null, "order_id", orderId);
        queryWrapper.eq(status != null, "status", status);
        queryWrapper.like(StringUtils.isNotBlank(flag), "flag", flag);
        queryWrapper.like(StringUtils.isNotBlank(route), "route", route);

        return queryWrapper;
    }

    @Override
    public Long addMenu(MenuAddRequest menuAddRequest) {
        if (menuAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Menu menu = new Menu();

        BeanUtils.copyProperties(menuAddRequest, menu);
        boolean result = this.save(menu);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return menu.getId();
    }

    @Override
    public Boolean updateMenu(MenuUpdateRequest menuUpdateRequest) {
        if (menuUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuUpdateRequest,menu);
        boolean result = this.updateById(menu);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    @Override
    public Boolean deleteMenu(DeleteRequest deleteRequest) {
        if (deleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = this.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    @Override
    public List<MenuVO> listMenuByPage() {
       return getMenuVO(this.list());
    }

    @Override
    public Boolean readExcel(MultipartFile multipartFile) {
        // 1.转换文件为字节流
        InputStream inputStream;
        try {
            inputStream = multipartFile.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 2.执行文件处理逻辑
        EasyExcel.read(inputStream, Menu.class, new MenutExcelListener()).sheet().doRead();
        // 3.入库
        boolean saveBatch = this.saveBatch(MenutExcelListener.CACHED_DATA_LIST);

        ThrowUtils.throwIf(!saveBatch, ErrorCode.PARAMS_ERROR);
        return true;
    }

    @Override
    public Boolean writeExcel(String path) {
        // 1.定义文件路径
        String fullPath = DOWNLOAD_PATH + AREA_TABLE_NAME;
        // 2.检查文件是否存在
        ExcelConstant.isExist(fullPath);
        // 3.根据路径导出
        List<MenuVO> menuList = this.listMenuByPage();
        EasyExcel.write(fullPath, User.class).sheet(AREA_SHEET_NAME).doWrite(menuList);
        return true;
    }
}




